Repository navigation
model: accept any UTC tzinfo for Edm.DateTime - #334
Open
VitorCoelho wants to merge 1 commit into
Open
VitorCoelho wants to merge 1 commit into
VitorCoelho wants to merge 1 commit into
Conversation
Edm.DateTime to_literal() and to_json() compared value.tzinfo with
datetime.timezone.utc. Only fixed-offset datetime.timezone objects compare
equal to it, so UTC datetimes built with zoneinfo.ZoneInfo('UTC'),
pytz.utc or dateutil.tz.UTC were rejected with the contradictory error
"Edm.DateTime accepts only UTC", both in $filter literals and in
create/update payloads.
Treat a datetime as UTC when its tzinfo equals datetime.timezone.utc (the
previous rule, unchanged) or when its offset is zero and its tzname() is
"UTC". Zones that only have a zero offset at that moment, such as
Europe/London in winter, are still rejected, so whether a value is
accepted does not depend on the season. Naive datetimes are still
rejected.
Signed-off-by: VitorCoelho <vitorc195@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Edm.DateTimeto_literal()andto_json()now accept UTC datetimes whosetzinfoiszoneinfo.ZoneInfo('UTC'),pytz.utcordateutil.tz.UTC. Before, these raisedPyODataModelError: Edm.DateTime accepts only UTC.Why
The check was
value.tzinfo != datetime.timezone.utc. Only fixed-offsetdatetime.timezoneobjects compare equal totimezone.utc, so a UTC datetime from the standardzoneinfomodule or from pytz/dateutil failed with an error message that says the opposite of what happened. This affects$filtervalues (filter(Date__gte=...)) and create/update payloads:How
A datetime is treated as UTC when its
tzinfoequalsdatetime.timezone.utc(the previous rule, so everything accepted before is still accepted) or when its offset is zero andtzname()is"UTC".I did not use a plain "offset is zero" rule: it would accept
Europe/Londonin winter and reject it in summer, so the same code would start failing in March. With this rule, London is rejected all year, as before. For the same reason zones named "GMT" (Etc/GMT,pytz.timezone('GMT')) are still rejected, as London is also called "GMT" in winter. Naive datetimes are still rejected. If you prefer the simpler offset-only rule, I can change it.Tests
test_traits_datetime_accepts_any_utc_tzinfo(timezone.utc, zero offset,ZoneInfo('UTC'),ZoneInfo('Etc/UTC'), a fixed UTC tzinfo like pytz/dateutil use): the 3 non-timezonecases fail on master.test_traits_datetime_rejects_non_utc: naive, +01:00, a tzinfo named "UTC" with an offset, Europe/London in summer and winter.pytz.utc,pytz.timezone('UTC'),dateutil.tz.UTC,tzutc(),gettz('UTC'),ZoneInfo('UTC'/'Etc/UTC'/'Zulu')are accepted in January and July; London, GMT, Abidjan and +01:00 are rejected in both. For 20,000 random datetimes the literal and JSON are identical totimezone.utc.pyodata.### Fixedheading with model: keep all digits of Edm.Double values in literals and payloads #332 if both are merged; whichever goes second just keeps both lines.